#include "stdheader.h"

/////////////////////////////////////////////////////////

CInGameScreen::CInGameScreen() 
{
}

/////////////////////////////////////////////////////////

CInGameScreen::~CInGameScreen()
{
	for (int i = 0; i < MAX_CHARS; ++i)
	{
		delete m_pCharacters[i];
	}

	delete m_pFarmer;
}

/////////////////////////////////////////////////////////

void CInGameScreen::Process(float fTimeDelta)
{
	// Technically ProcessInput-type code, but needs to be here for fluid movement 
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
		m_pActiveChar->MoveUp(fTimeDelta);

	else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
		m_pActiveChar->MoveDown(fTimeDelta);

	else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
		m_pActiveChar->MoveLeft(fTimeDelta);

	else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
		m_pActiveChar->MoveRight(fTimeDelta);

	else 
		m_pActiveChar->Idle();

	if (m_pActiveChar)
	{
		m_pActiveChar->Update(fTimeDelta);
	}

	m_pFarmer->Update(fTimeDelta);
}

/////////////////////////////////////////////////////////

void CInGameScreen::Render(sf::RenderWindow* pWin)
{
	v2f vCentre(m_pActiveChar->GetPosition().x + m_pActiveChar->GetSprite().getGlobalBounds().width/2,
		m_pActiveChar->GetPosition().y + m_pActiveChar->GetSprite().getGlobalBounds().height/2);

	sf::View view = pWin->getDefaultView();
	view.setCenter(vCentre);
	view.zoom(GetCharZoom());
	pWin->setView(view);

	for (int i = 0; i < 36; i++)
	{
		pWin->draw(m_tGrassSprites[i]);
	}
	
	CBaseScreen::Render(pWin);

	for (int i = 0; i < MAX_CHARS; i++)
	{
		m_pCharacters[i]->Render(pWin);
	}

	m_pFarmer->Render(pWin);
}

/////////////////////////////////////////////////////////

void CInGameScreen::ProcessInput(sf::Event& tEvent)
{
	if (tEvent.type == sf::Event::KeyPressed && tEvent.key.code == sf::Keyboard::Q)
	{
		int iChar = CGame::Instance()->GetActiveChar();
		if (iChar > 0) 
			iChar--;
		else
			iChar = MAX_CHARS-1;

		CGame::Instance()->SetActiveChar(iChar);
		m_pActiveChar = m_pCharacters[iChar];
	}

	if (tEvent.type == sf::Event::KeyPressed && tEvent.key.code == sf::Keyboard::E)
	{
		int iChar = CGame::Instance()->GetActiveChar();
		if (iChar < MAX_CHARS-1) 
			iChar++;
		else
			iChar = 0;

		CGame::Instance()->SetActiveChar(iChar);
		m_pActiveChar = m_pCharacters[iChar];
	}
}

/////////////////////////////////////////////////////////

void CInGameScreen::LayoutScreen()
{
	CBaseScreen::LayoutScreen();

	m_tGrassTex.loadFromFile("Resources/Art/GrassTile.jpg");

	for (int i = 0; i < 36; i++)
	{
		m_tGrassSprites[i].setTexture(m_tGrassTex);
		m_tGrassSprites[i].setScale(0.5f, 0.5f);
	}

	v2f vOffset = v2f(SC_WIDTH/2 - m_tGrassSprites[0].getGlobalBounds().width * 3,
						SC_HEIGHT/2 - m_tGrassSprites[0].getGlobalBounds().height * 3);

	for (int i = 0; i < 6; i++)
	{
		vOffset.y = SC_HEIGHT/2 - m_tGrassSprites[0].getGlobalBounds().height * 3;

		for (int j = 0; j < 6; j++)
		{
			m_tGrassSprites[(i * 6) + j].setPosition(vOffset);
			vOffset.y += m_tGrassSprites[0].getGlobalBounds().height;
		}

		vOffset.x += SC_WIDTH/2 - m_tGrassSprites[0].getGlobalBounds().width;
	}

	m_pCharacters[EActiveChar_Dog] = new CDoge();
	m_pCharacters[EActiveChar_Ox] = new COx();
	m_pCharacters[EActiveChar_Rat] = new CRat();
	m_pCharacters[EActiveChar_Rooster] = new CRooster();
	
	m_pFarmer = new CFarmer();

	m_pActiveChar = m_pCharacters[CGame::Instance()->GetActiveChar()];
	StartFadeIn();
}

/////////////////////////////////////////////////////////

float CInGameScreen::GetCharZoom()
{
	switch (CGame::Instance()->GetActiveChar())
		{
		case EActiveChar_Dog:
			return DOG_ZOOM;
		case EActiveChar_Ox:
			return OX_ZOOM;
		case EActiveChar_Rat:
			return RAT_ZOOM;
		case EActiveChar_Rooster:
			return ROOSTER_ZOOM;
		default:
			return 1.0f;
		}
}